package persistency.writer;

import model.FreeStudent;
import model.RetrainingStudent;
import model.Student;
import settings.PathSettings;

public class StudentWriter extends ContainerWriter {

	private AssociationWriter courseHistoryWriter = new CourseHistoryWriter();
	private Writer freeStudentWriter = new FreeStudentWriter();
	private RetrainingStudentWriter retrainingStudentWriter = new RetrainingStudentWriter();
	
	/**
	 * instantiate the StudentWriter
	 */
	public StudentWriter() {
		super(PathSettings.STUDENTFILE, "#Number\tName\t\n");
		
	}
	@Override
	public void write(Writable writable) {
		Student student = (Student) writable;
		super.write(student.getStudentNumber() + "\t" + student.getName() + "\n");
		courseHistoryWriter.write(writable.getPrimaryKey(),student.getCourseHistories());
		
		//test for FreeStudent or Retraining student
		if (student instanceof FreeStudent) {
			freeStudentWriter.write(student);

		} else if (student instanceof RetrainingStudent) {
			
			retrainingStudentWriter.write(student);
		}
	}
	
	@Override
	public void close(){
		super.close();
		courseHistoryWriter.close();
		freeStudentWriter.close();
		retrainingStudentWriter.close();
	}
	
	/**
	 * Inner class that handles the Association between Student and CourseHistory
	 * @author Wim
	 *
	 */
	private class CourseHistoryWriter extends AssociationWriter {
		public CourseHistoryWriter(){
			super(PathSettings.COURSEHISTORYFILE,"#StudentCode\tCourseCode\tSchoolYear\tScore\n");
		}
		
	}
	/**
	 * Inner class that handles the FreeStudent data
	 * @author Wim
	 *
	 */
	private class FreeStudentWriter extends Writer {
		public FreeStudentWriter(){
			super(PathSettings.FREESTUDENTFILE,"#StudentCode\tNationality\n");
		}
	}
	
	/**
	 * Inner class that handles the RetraingStudent data
	 * @author Wim
	 *
	 */
	private class RetrainingStudentWriter extends Writer {
		public RetrainingStudentWriter(){
			super(PathSettings.RETRAININGSTUDENTFILE,"#StudentCode\tHighestDegree\tRetrainingReason\n");
		}
	}
	
	

}
